import java.util.*;

public class BinaryTree {
    //定义结点
    static class TreeNode {
        public char val;
        public TreeNode left;
        public TreeNode right;

        public TreeNode(char val) {
            this.val = val;
        }
    }
    //创建一个树
    public TreeNode createTree() {
        TreeNode A = new TreeNode('A');
        TreeNode B = new TreeNode('B');
        TreeNode C = new TreeNode('C');
        TreeNode D = new TreeNode('D');
        TreeNode E = new TreeNode('E');
        TreeNode F = new TreeNode('F');
        TreeNode G = new TreeNode('G');
        TreeNode H = new TreeNode('H');
        A.left = B;
        A.right = C;
        B.left = D;
        B.right = E;
        C.left = F;
        C.right = G;
        E.right = H;
        return A;
    }
    //前序遍历
    public void preOrder(TreeNode root) { //root一直在改变
        if (root == null) {
            return;
        }
        System.out.print(root.val + " ");
        preOrder(root.left);
        preOrder(root.right);
    }

    //将前序遍历，存储到list当中
//    List<TreeNode> list = new ArrayList<>();
//    public List<TreeNode> preOrder1(TreeNode root) { //root一直在改变
//        if (root == null) {
//            return list;
//        }
//        list.add(root);
//
//        //返回值没人接收，没有合理利用返回值
//        preOrder1(root.left);
//        preOrder1(root.right);
//
//        return list;
//    }
    public List<TreeNode> preOrder1(TreeNode root) { //root一直在改变
        List<TreeNode> list = new ArrayList<>();
        if (root == null) {
            return list;
        }
        list.add(root);
        List<TreeNode> leftTree = preOrder1(root.left);
        list.addAll(leftTree);
        List<TreeNode> rightTree = preOrder1(root.right);
        list.addAll(rightTree);
        return list;
    }

    //中序遍历
    public void inOrder(TreeNode root) { //root一直在改变
        if (root == null) {
            return;
        }
        inOrder(root.left);
        System.out.print(root.val + " ");
        inOrder(root.right);
    }
    public List<TreeNode> inOrder1(TreeNode root) { //root一直在改变
        List<TreeNode> list = new ArrayList<>();
        if (root == null) {
            return list;
        }
        List<TreeNode> leftTree = inOrder1(root.left);
        list.addAll(leftTree);
        list.add(root);
        List<TreeNode> rightTree = inOrder1(root.right);
        list.addAll(rightTree);
        return list;
    }

    //后序遍历
    public void postOrder(TreeNode root) { //root一直在改变
        if (root == null) {
            return;
        }
        postOrder(root.left);
        postOrder(root.right);
        System.out.print(root.val + " ");
    }
    public List<TreeNode> postOrder1(TreeNode root) { //root一直在改变
        List<TreeNode> list = new ArrayList<>();
        if (root == null) {
            return list;
        }
        List<TreeNode> leftTree = postOrder1(root.left);
        list.addAll(leftTree);
        List<TreeNode> rightTree = postOrder1(root.right);
        list.addAll(rightTree);
        list.add(root);
        return list;
    }

    //获取树中结点的个数
    public int size = 0;
    public int nodeSize(TreeNode root) {
        //以遍历的方式
        if (root == null) {
            return 0;
        }
        size++;
        nodeSize(root.left);
        nodeSize(root.right);
        return size;
    }
    public int nodeSize1(TreeNode root) {
        //以子树的方式
        if (root == null) {
            return 0;
        }
        return nodeSize1(root.left) + nodeSize1(root.right) + 1;
    }

    //获取叶子结点的个数
    public int leafSize = 0;
    public int getLeafSize(TreeNode root) {
        //以遍历的方式
        if (root == null) {
            return 0;
        }
        if (root.left == null && root.right == null) {
            leafSize++;
        }
        getLeafSize(root.left);
        getLeafSize(root.right);
        return leafSize;
    }
    public int getLeafSize1(TreeNode root) {
        //以子树的方式
        if (root == null) {
            return 0;
        }
        if (root.left == null && root.right == null) {
            return 1;
        }
        getLeafSize(root.left);
        getLeafSize(root.right);
        return getLeafSize1(root.left) + getLeafSize1(root.right);
    }

    //获取第K层结点的个数
    public int getKLevelNodeSize(TreeNode root, int k) {
        //以子树的方式
        if (root == null) {
            return 0;
        }
        if (k == 1) {
            return 1;
        }
        return getKLevelNodeSize(root.left, k - 1) +
                getKLevelNodeSize(root.right, k - 1);
    }

    //获取二叉树的高度
    public int getHigh(TreeNode root) {
        //以子树的方式
        if (root == null) {
            return 0;
        }
        int leftHigh = getHigh(root.left);
        int rightHigh = getHigh(root.right);
        return leftHigh > rightHigh ? leftHigh + 1 : rightHigh + 1;
    }

    //查找值为key的元素是否存在
    public boolean find(TreeNode root, char key) {
        if (root == null) {
            return false;
        }
        if (root.val == key) {
            return true;
        }
        boolean leftKey = find(root.left, key);
        if (leftKey == true) return true;

        boolean rightKey = find(root.right, key);
        if (rightKey == true) return true;

        return false;
    }

    //层序遍历
    public void levelOrder(TreeNode root) {
        if (root == null) return;
        //用队列存放
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while (!queue.isEmpty()) {
            TreeNode cur = queue.poll();
            System.out.print(cur.val + " ");
            if (cur.left != null) {
                queue.offer(cur.left);
            }
            if (cur.right != null) {
                queue.offer(cur.right);
            }
        }
    }
    //一层一个队列
    public List<List<TreeNode>> levelOrder1(TreeNode root) {
        List<List<TreeNode>> ret = new ArrayList<>();
        if (root == null) return ret;
        //用队列存放
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while (!queue.isEmpty()) {
            int size = queue.size(); //计算当前队列长度
            List<TreeNode> tmp = new ArrayList<>(); //每一层的队列
            while (size != 0) {
                TreeNode cur = queue.poll();
                tmp.add(cur);
                size--;
                if (cur.left != null) {
                    queue.offer(cur.left);
                }
                if (cur.right != null) {
                    queue.offer(cur.right);
                }
            }
            ret.add(tmp);
        }
        return ret;
    }

    //判断是否为完全二叉树：当有一个数据为空时，后面的数据必须全为空，才是完全二叉树
    public boolean isCompleteTree(TreeNode root) {
        if (root == null) return true;
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while (!queue.isEmpty()) {
            TreeNode cur = queue.poll();
            if (cur != null) {
                queue.offer(cur.left);
                queue.offer(cur.right);
            } else {
                //遇见第一个null
                break;
            }
        }
        while (!queue.isEmpty()) {
            TreeNode cur = queue.poll();
            if (cur != null) {
                return false;
            }
        }
        return true;
    }

    //前序遍历非递归
    public void preOrderNor(TreeNode root) { //root一直在改变
        if (root == null) {
            return;
        }
        Stack<TreeNode> stack = new Stack<>();
        TreeNode cur = root;
        TreeNode top = null;
        while (cur != null || !stack.isEmpty()) {
            while (cur != null) {
                stack.push(cur);
                System.out.print(cur.val + " ");
                cur = cur.left;
            }
            top = stack.pop();
            cur = top.right;
        }
    }

    //中序遍历非递归
    public void inOrderNor(TreeNode root) { //root一直在改变
        if (root == null) {
            return;
        }
        Stack<TreeNode> stack = new Stack<>();
        TreeNode cur = root;
        TreeNode top = null;
        while (cur != null || !stack.isEmpty()) {
            while (cur != null) {
                stack.push(cur);
                cur = cur.left;
            }
            top = stack.pop();
            System.out.print(top.val + " ");
            cur = top.right;
        }
    }

    //后序遍历非递归
    public void postOrderNor(TreeNode root) { //root一直在改变
        if (root == null) {
            return;
        }
        Stack<TreeNode> stack = new Stack<>();
        TreeNode cur = root;
        TreeNode top = null;
        TreeNode prev = null;
        while (cur != null || !stack.isEmpty()) {
            while (cur != null) {
                stack.push(cur);
                cur = cur.left;
            }
            top = stack.peek(); //后序遍历最后打印根结点
            if (top.right == null || top.right == prev /* 防止死循环 */) {
                stack.pop();
                System.out.print(top.val + " ");
                prev = top; //证明当前结点已经被打印了
            } else {
                cur = top.right;
            }
        }
    }

}
